<template>
  <div>
    <el-form :inline="true" :model="searchMap" class="demo-form-inline">
      <el-form-item label="para编号">
        <el-input v-model="searchMap.id" placeholder="请输入para编号"></el-input>
      </el-form-item>
      <el-form-item label="para名称">
        <el-input v-model="searchMap.name" placeholder="请输入para名称"></el-input>
      </el-form-item>
      <!-- <el-form-item label="options">
        <el-input v-model="searchMap.options" placeholder="请输入options"></el-input>
      </el-form-item>
      <el-form-item label="seq">
        <el-input v-model="searchMap.seq" placeholder="请输入seq"></el-input>
      </el-form-item> -->
      <el-form-item>
        <el-button type="primary" @click="onSubmit">查询</el-button>
        <el-button type="primary" @click="goBack">返回</el-button>
        <el-button type="primary" @click="dialogFormVisible=true; paraForm={templateId:templateId}">新增para</el-button>
      </el-form-item>
    </el-form>
    <el-table :data="tableData" style="width: 100%">
      <el-table-column label="编号" width="180" prop="id"></el-table-column>
      <el-table-column label="名称" width="180" prop="name"></el-table-column>
      <el-table-column label="options" width="180" prop="options"></el-table-column>
      <el-table-column label="seq" width="180" prop="seq"></el-table-column>
      <el-table-column label="templateName" width="180">
        <template slot-scope="scope">
            <span  type="success" v-for="temp in tempList" :key="temp.id">
              <el-tag v-if="scope.row.templateId === temp.id">
                  {{temp.name}}
              </el-tag>
            </span>
         </template>
         </el-table-column>
      <el-table-column label="操作">
        <template slot-scope="scope">
          <el-button size="mini" @click="handleEdit(scope.$index, scope.row)">编辑</el-button>
          <el-button size="mini" type="danger" @click="handleDelete(scope.$index, scope.row)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <el-pagination
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
      :current-page="currentPage"
      :page-sizes="pageSizeList"
      :page-size="pageSize"
      layout="total, sizes, prev, pager, next, jumper"
      :total="total"
    ></el-pagination>

    <el-dialog :title="paraForm.id==null?'新增':'编辑'" :visible.sync="dialogFormVisible">
      <el-form :model="paraForm">
        <el-form-item label="名称" :label-width="formLabelWidth">
          <el-input v-model="paraForm.name" autocomplete="off"></el-input>
         </el-form-item>
        <el-form-item label="options" :label-width="formLabelWidth">
          <el-input  type="textarea" :autosize="{minRows: 2, maxRows: 4}" v-model="paraForm.options" autocomplete="off"></el-input>
         </el-form-item>
        <el-form-item label="seq" :label-width="formLabelWidth">
          <el-input v-model="paraForm.seq" autocomplete="off"></el-input>
         </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible = false; paraForm={}">取 消</el-button>
        <el-button type="primary" @click="save">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
export default {
  data() {
    return {
      tableData: [],
      currentPage: 1,
      pageSize: 10,
      pageSizeList: [5, 10, 20, 30],
      total: 0,
      searchMap: {},
      dialogFormVisible: false,
      paraForm: {},
      xian: {},
      formLabelWidth: "150px",
      tempList: [],
      templateId: 0
    }
  },
  methods: {
    async getTempList() {
      const { data: res } = await this.$http.get(
        'goods/template/findAll'
      );
      this.tempList = res.data;
    },
    async getList() {
      console.log(this.searchMap)
      const { data: res } = await this.$http.post(
        `goods/para/findPage/${this.currentPage}/${this.pageSize}`,
        this.searchMap
      );
      this.tableData = res.data.rows;
      this.total = res.data.total;
    },
    onSubmit() {
      this.currentPage = 1;
      this.getList();
    },
    async handleEdit(index, row) {
      this.dialogFormVisible = true;
      const id = row.id;
      const { data: res } = await this.$http.get(`goods/para/findById/${id}`);
      this.paraForm = res.data;
        this.paraForm.options = this.paraForm.options.replace(/,/g, '\n')
    },
    async handleDelete(index, row) {
      const id = row.id;
      this.$confirm("此操作将永久删除该记录, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      })
        .then(async () => {
          const { data: res } = await this.$http.post(`goods/para/delete/${id}`);
          if (res.code === 200) {
            this.$message({
              type: "success",
              message: "删除成功!"
            });
            this.getList();
          }
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消删除"
          });
        });
    },
    async handleSizeChange(val) {
      this.pageSize = val;
      this.getList();
    },
    async handleCurrentChange(val) {
      this.currentPage = val;
      this.getList();
    },
    async save() {
      this.paraForm.options = this.paraForm.options.replace(/\n/g, ',')
      this.dialogFormVisible = false;
      const { data: res } = await this.$http.post(
        `goods/para/${this.paraForm.id == null ? "insert" : "update"}`,
        this.paraForm
      );
      if (res.code === 200) {
        this.$message({
          type: "success",
          message: "操作成功!"
        });
        this.getList();
      }
    },
    async dd() {
      const { data: res } = await this.$http.get(
        `goods/para/page/${this.currentPage}/${this.pageSize}`
      );
      console.log(res);
      this.xian = res.data.list;
      // 集合转数组
      // for (var i in this.xian) {
      //   this.tableData.push(this.xian[i])
      // }
      this.tableData = Object.values(this.xian);
      this.total = res.data.total;
    },
    goBack() {
      this.$router.go(-1)
    }
  },
  async created() {
    const id = this.$route.query.id;
    this.templateId = id
    this.searchMap = { templateId: id }
    this.getTempList();
    this.getList();
  }
};
</script>

<style scoped>
.avatar-uploader .el-upload {
  border: 1px dashed #d9d9d9;
  border-radius: 6px;
  cursor: pointer;
  position: relative;
  overflow: hidden;
}
.avatar-uploader .el-upload:hover {
  border-color: #409eff;
}
.avatar-uploader-icon {
  font-size: 28px;
  color: #8c939d;
  width: 178px;
  height: 178px;
  line-height: 178px;
  text-align: center;
}
.avatar {
  width: 250px;
  height: 178px;
  display: block;
}
</style>
